package com.usabb.pojo.vo;


import com.usabb.utils.ListUtils;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Builder
@Setter
@Getter
@ToString
public class CombosCategory {
    private String category;
    /**
     * 服务类目
     */
    private Integer categoryId;
    /**
     * 服务类目Id
     */
    private List<CombosCategoryItem> items;



    public static List<CombosCategory> of(List<String> categoryNames) {
        return categoryNames.stream()
                .map(it -> CombosCategory.builder().category(it).build())
                .collect(Collectors.toList());
    }


    public static List<CombosCategory> from(CombosAddVO addVO) {
        final List<String> categoryNames = addVO.getCategory();
        final String[] categoryItemCounts = StringUtils.split(addVO.getCategoryTrCount(), CombosAddVO.DELIMITER);  //每个类目下个item个数
        if (categoryNames.size() != categoryItemCounts.length) {
            log.error("Input something wrong. categoryNames.size: {}, itemCounts: {}",
                    categoryNames.size(), categoryItemCounts.length);
            return null;
        }

        final List<Integer> trCountList = Arrays.stream(categoryItemCounts)
                .map(it -> Integer.valueOf(it))
                .collect(Collectors.toList());

        final List<List<String>> itemNoSlices = ListUtils.slice(addVO.getItemNo(), trCountList);
        final List<List<String>> itemNameSlices = ListUtils.slice(addVO.getItemName(), trCountList);
        final List<List<String>> itemRemarkSlices = ListUtils.slice(addVO.getItemRemark(), trCountList);
        final List<List<String>> itemIntroSlices = ListUtils.slice(addVO.getItemIntro(), trCountList);

        List<CombosCategory> categories = CombosCategory.of(addVO.getCategory());
        for (int i=0; i<categories.size(); i++) {
            final CombosCategory category = categories.get(i);

            final List<String> itemNoList = itemNoSlices.get(i);
            final List<String> itemNameList = itemNameSlices.get(i);
            final List<String> itemRemarkList = itemRemarkSlices.get(i);
            final List<String> itemIntroList = itemIntroSlices.get(i);

            List<CombosCategoryItem> itemList = CombosCategoryItem
                    .ofItems(itemNoList, itemNameList, itemRemarkList, itemIntroList);
            category.setItems(itemList);
        }

        return categories;
    }

}
